package com.atguigu.kafka.flink;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.util.Arrays;
import java.util.HashMap;

public class WikipediaAnalysis {
    public static void main(String[] args) throws Exception {
        //第一步获取执行环境
//        ExecutionEnvironment env=ExecutionEnvironment.getExecutionEnvironment();
        //这个是获取流式执行环境，flink1.12之后，流式和批处理执行环境统一
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //指定为自动模式:AUTOMATIC。此设置后,flink将会自动识别数据源类型
        //有界数据流，则会采用批方式进行数据处理
        //无界数据流，则会采用流方式进行数据处理
        //RuntimeExecutionMode.BATCH  批处理模式  RuntimeExecutionMode.STREAMING  流处理模式
        env.setRuntimeMode(RuntimeExecutionMode.STREAMING);
        //设置3个并行度
        env.setParallelism(4);
        //数据流的任意一点，可以对数据进行转换
        DataStream<String> elementsSource = env.fromElements("java,scala,php,c++","java,scala,php", "java,scala", "java");
//        DataStreamSource<String> elementsSource = env.fromCollection(Arrays.asList("java,scala,php,c++", "java,scala,php", "java,scala", "java"));
        //数据流的起始点
//        DataStreamSource<String> dataStreamSource= env.socketTextStream("10.254.131.147", 9092);

        // 数据处理
        DataStream<String> flatMap = elementsSource.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String element, Collector<String> out) throws Exception {
                String[] wordArr = element.split(",");
                for (String word : wordArr) {
                    out.collect(word);
                }
            }
        });
        SingleOutputStreamOperator<Tuple2<String, Integer>> source = flatMap.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                return Tuple2.of(value, 1);
            }
        });

        //SingleOutputStreamOperator DataStream的子类
//        SingleOutputStreamOperator<String> source = flatMap.map(new MapFunction<String, String>() {
//            @Override
//            public String map(String value) throws Exception {
//                return value.toUpperCase();
//            }
//        });

        // 4.数据输出
        source.print();
        // 5.执行程序
        env.execute("flink-hello-world");
    }
}
